﻿using System.Collections.Generic;
using System.IO;
using System.Web;

namespace CommonMethod
{
    public static class FileHelper
    {
        #region 文件转二进制
        /// <summary>
        /// 文件转二进制
        /// http文件不能写入
        /// </summary>
        public static void FileToByte()
        {
            HttpRequest request = HttpContext.Current.Request;
            if (request.Files.Count > 0)
            {
                long length = 0;
                List<Stream> streams = new List<Stream>();
                for (int i = 0; i < request.Files.Count; i++)
                {
                    Stream stream = request.Files[i].InputStream;
                    //byte[] buffer = Encoding.Unicode.GetBytes(",");
                    //StreamWriter streamWriter = new StreamWriter(stream);
                    //streamWriter.Write(",");
                    //stream.Write(buffer,0,buffer.Length);
                    streams.Add(stream);
                    //Stream stream = request.Files[i].InputStream;
                    length += streams[i].Length;
                    //byte[] buffer = new byte[stream.Length];
                    //stream.Read(buffer, 0, buffer.Length);
                }

                //productRecord.ATTACHMENTS = buffer;
                //ATTACHMENTS.Add(new ATTACHMENT() { FileName = request.Files[i].FileName, AByte = buffer });
                int streamlength = 0;
                byte[] fileBuffer = new byte[length];
                foreach (Stream item in streams)
                {
                    item.Read(fileBuffer, streamlength, (int)item.Length);
                    streamlength += (int)item.Length;
                    item.Close();
                }
            }
        }
        #endregion



    }
}